package com.loan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loan.entity.Application;
import com.loan.entity.Emp;
import com.loan.entity.Handle;
import com.loan.entity.Process;
import com.loan.exception.ApplicationException;
import com.loan.exception.EmpException;
import com.loan.exception.HandleException;
import com.loan.mappers.ApplicationMapper;
import com.loan.mappers.EmpMapper;
import com.loan.mappers.HandleMapper;
import com.loan.mappers.ProcessMapper;
import com.loan.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerMapping;

import java.util.Date;

/**
 * @ClassName: ProcessServiceImpl
 * @Description: 申请流转表业务层
 * @Author: wYin
 * @Date: 2022/1/17 22:17
 */
@Service("processService")
public class ProcessServiceImpl extends ServiceImpl<ProcessMapper, Process> implements ProcessService {
    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private HandleMapper handleMapper;

    @Autowired
    private ProcessMapper processMapper;

    /**
     * 功能描述: <br>
     * 用于记录申请表的流转过程，其中需要进行对申请表的校验、员工的校验、以及操作的校验
     * @Param: [process]
     * @Return: boolean
     * @Author: wYin
     * @Date: 2022/1/17 22:43
     */
    @Override
    public boolean insert(Process process) {
        //判断申请表信息是否存在
        Application application = applicationMapper.selectById(process.getAppId());
        if(application==null){
            throw  new ApplicationException("当前操作的申请表不存在");
        }
        //判断当前操作的业务员是否存在
        Emp emp = empMapper.selectById(process.getEmpId());
        if(emp==null){
            throw new EmpException("当前操作的业务员不存在");
        }
        //判断当前的操作是否符合要求
        Handle handle = handleMapper.selectById(process.getHandleId());
        if(handle==null){
            throw  new HandleException("当前的操作不符合要求，没有该类型的操作");
        }
        process.setTime(new Date());
        //执行申请流转表数据添加
        int row = processMapper.insert(process);
        if(row>0){
            return true;
        }
        return false;
    }
}
